<!--工作日志-->
<style lang="less">
</style>

<template>
  <div class="river">
    <Card :bordered="false">
      <p slot="title">日志 > <span class="select-font">工作日志</span></p>
      <div>
        <Form :model="searchItem"
              class="smallForm">
          <Date-picker type="daterange"
                       split-panels
                       placeholder="请选择日志时间"
                       format="yyyy-MM-dd"
                       :value="searchItem.time"
                       @on-change="chooseTime" />
        </Form>
        <Button type="info"
                style="margin-left:25px"
                @click="getData('1')">搜索</Button>
        <Button type="success"
                @click="downloadData"
                class="pull-right">导出日志数据</Button>
      </div>
    </Card>
    <div>
      <Table ref="table"
             :data="logData"
             :columns="logColumns"
             stripe></Table>
      <Page class="pull-right"
            :total="Page.total"
            :current="Page.current"
            :page-size="Page.pageSize"
            size="small"
            show-total
            @on-change="changePage"></Page>
    </div>
  </div>
</template>

<script>
import { ajaxPost } from 'global/axios';
import storage from 'good-storage';
export default {
  data() {
    return {
      urlLogList: '/sys/userlog/list',
      searchItem: {
        time: [],
      },
      logData: [],
      logColumns: [
        {
          title: '操作',
          key: 'action',
        },
        {
          title: '时间',
          key: 'date_time',
        },
        {
          title: '操作对象',
          key: 'target',
        },
        {
          title: '操作人',
          key: 'realname',
        },
        {
          title: '类型',
          key: 'type',
        },
      ],
      Page: {
        current: 1,
        pageSize: 13,
        total: 0,
      },
      selectData: [],
      actionPage: '',
    };
  },
  methods: {
    chooseTime(val) {
      this.searchItem.time = val;
    },

    getData(page = 1) {
      let params = {
        data: {
          page,
          pageSize: this.Page.pageSize,
          searchValue: {},
          order: { date_time: -1 },
          basis: 2,
        },
        url: this.urlLogList,
        token: storage.session.get('token'),
        success: res => {
          this.$Loading.finish();
          this.logData = res.data.result.result;
          this.Page.total = res.data.result.totalSize;
        },
      };
      if (this.searchItem.time.length > 0 && this.searchItem.time[0] !== '') {
        params.data.searchValue.date_time = `${this.searchItem.time[0]} 00:00:00|#|${this.searchItem.time[1]} 23:59:59`;
      }
      ajaxPost(params);
    },
    changePage(page) {
      this.getData(page);
      this.actionPage = page;
    },
    downloadData() {
      if (this.searchItem.time.length === 0) {
        this.$Message.warning('请选择日志时间');
        return false;
      }
      let params = {
        data: {
          page: 1,
          searchValue: {},
        },
        url: this.urlLogList,
        token: storage.session.get('token'),
        success: res => {
          this.$refs.table.exportCsv({
            filename: '工作日志数据',
            data: res.data.result.result,
            columns: this.logColumns,
          });
        },
      };
      if (this.searchItem.time[0] !== '') {
        params.data.searchValue.date_time = `${this.searchItem.time[0]} 00:00:00|#|${this.searchItem.time[1]} 23:59:59`;
      }
      ajaxPost(params);
    },
  },
  mounted() {
    this.getData();
  },
};
</script>
